// v2010.09.21

/*
Copyright (c) 2010 Ben Hendel-Doying

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package world;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

//800x600 display
//20x20 graphics

//40x30 map area shown

final public class World
{
	public static Random RNG = new Random(System.currentTimeMillis());

	private HashMap<Integer, HashMap<Integer, Tile>> world = new HashMap<Integer, HashMap<Integer, Tile>>();
	private ArrayList<Actor> monsters = new ArrayList<Actor>();
	private Actor player = null;
	private int tile_count = 0;
	private int step_count = 0;

	public World(Actor PC)
	{
		player = PC;

		Tile t = new Tile(this, player, 0, 0, 5, 0, false);
		tile_count++;

		HashMap<Integer, Tile> row = new HashMap<Integer, Tile>();
		row.put(0, t);
		world.put(0, row);

		CreateEmptyAdjacentTiles(t); // ensure that there's empty space in the 8 surrounding tiles

		t.AddCreature(player);
		player.Explore();

		Gate g = new Gate();

		player.MemorizeGate(g);

		switch(World.RNG.nextInt(4))
		{
			case 0: t.GetNorth().AddScenery(g); break;
			case 1: t.GetEast().AddScenery(g); break;
			case 2: t.GetSouth().AddScenery(g); break;
			case 3: t.GetWest().AddScenery(g); break;
		}
	}

	public void Render(Graphics g, Camera c, int offset_x, int offset_y)
	{
		for(int y = c.GetY(); y <= c.GetY2(); ++ y)
		{
			HashMap<Integer, Tile> row = world.get(y);

			if(row == null)
				continue;

			for(int x = c.GetX(); x <= c.GetX2(); ++x)
			{
				Tile t = row.get(x);

				if(t == null)
					continue;

				t.Render(g, x - c.GetX() + offset_x, y - c.GetY() + offset_y);
			}
		}
	}

	public Tile GetTile(int x, int y)
	{
		HashMap<Integer, Tile> row = world.get(y);

		if(row == null)
			return null;
		else
			return row.get(x);
	}

	private boolean DisparateDemonism(Tile a, Tile b)
	{
		return(a != null && b != null && Math.abs(a.GetMaxDemonism() - b.GetMaxDemonism()) >= 20);
	}

	private void WallOffDisparateDemonisms(Tile t)
	{
		if(
			DisparateDemonism(t, t.GetNorth()) ||
			DisparateDemonism(t, t.GetNE()) ||
			DisparateDemonism(t, t.GetEast()) ||
			DisparateDemonism(t, t.GetSE()) ||
			DisparateDemonism(t, t.GetSouth()) ||
			DisparateDemonism(t, t.GetSW()) ||
			DisparateDemonism(t, t.GetWest()) ||
			DisparateDemonism(t, t.GetNW())
		)
			t.AddScenery(new Wall());
	}

	public void CreateAdjacentTiles(Tile t, boolean not_empty)
	{
		final Tile n = CreateTile(t, t.GetX(), t.GetY() - 1, not_empty);
		final Tile e = CreateTile(t, t.GetX() + 1, t.GetY(), not_empty);
		final Tile s = CreateTile(t, t.GetX(), t.GetY() + 1, not_empty);
		final Tile w = CreateTile(t, t.GetX() - 1, t.GetY(), not_empty);

		// === prevent tight diagonals === //

		if(n.GetScenery() != null && e.GetScenery() != null)
			CreateTile(null, t.GetX() + 1, t.GetY() - 1, not_empty);
		else
			CreateTile(t, t.GetX() + 1, t.GetY() - 1, not_empty);

		if(e.GetScenery() != null && s.GetScenery() != null)
			CreateTile(null, t.GetX() + 1, t.GetY() + 1, not_empty);
		else
			CreateTile(t, t.GetX() + 1, t.GetY() + 1, not_empty);

		if(s.GetScenery() != null && w.GetScenery() != null)
			 CreateTile(null, t.GetX() - 1, t.GetY() + 1, not_empty);
		else
			CreateTile(t, t.GetX() - 1, t.GetY() + 1, not_empty);

		if(w.GetScenery() != null && n.GetScenery() != null)
			CreateTile(null, t.GetX() - 1, t.GetY() - 1, not_empty);
		else
			CreateTile(t, t.GetX() - 1, t.GetY() - 1, not_empty);
	}

	public void CreateEmptyAdjacentTiles(Tile t)
	{
		CreateAdjacentTiles(t, false);
	}

	private Tile CreateTile(Tile s, int x, int y, boolean not_empty)
	{
		int distance = 0;
		int demonism = 0;

		if(s != null)
		{
			distance = s.GetMinimumDistanceFromWall() - 1;
			demonism = s.GetMaxDemonism();
		}

		HashMap<Integer, Tile> row = world.get(y);

		if(row == null)
		{
			Tile t = new Tile(this, player, x, y, distance, demonism, not_empty);
			tile_count++;

			row = new HashMap<Integer, Tile>();
			row.put(x, t);
			world.put(y, row);

			WallOffDisparateDemonisms(t);

			return t;
		}
		else
		{
			Tile t = row.get(x);

			if(t == null)
			{
				t = new Tile(this, player, x, y, distance, demonism, not_empty);
				tile_count++;

				row.put(x, t);

				WallOffDisparateDemonisms(t);
			}

			return t;
		}
	}

	public void AddMonster(Actor a) { monsters.add(a); }
	public void RemoveMonster(Actor a) { monsters.remove(a); }

	public int GetStepCount() { return step_count; }

	public void Step()
	{
		step_count++;

		Iterator<Actor> i = monsters.iterator();

		while(i.hasNext())
		{
			Actor a = i.next();

			a.Step();
			a.Hunt(player);
		}

		player.Step();
	}

	public int TileCount() { return tile_count; }

	public Item CreateMinorOneOff()
	{
		final int a = RNG.nextInt(100);

		if(a < 60) // 60%
			return(new PotionHealth());
		else
		{
			switch(RNG.nextInt(9))
			{
				case 0: return(new PotionAttack());
				case 1: return(new PotionDodge());
				case 2: return(new PotionPray());
				case 3: return(new ScrollGehenna());
				case 4: return(new ScrollMending());
				case 5: return(new PotionFire());
				case 6: return(new PotionLight());
				case 7: return(new PotionDark());
				default: return(new PotionResistance());
			}
		}
	}

	public Item CreateMajorOneOff()
	{
		switch(RNG.nextInt(2))
		{
			case 0: return(new PotionPermanentHP());
			default: return(new ScrollGate());
		}
	}

	public Item CreateTreasure(int l)
	{
		final int a = RNG.nextInt(100);

		if(a < 60) // 60%
			return CreateMinorOneOff();
		else if(a < 70) // 10%
			return(new Sword(l));
		else if(a < 85) // 15%
			return(new Shield(l));
		else if(a < 95) // 10%
			return(new Robes(l));
		else // 5%
			return CreateMajorOneOff();
	}

	public Equipment CreateEquipment(int l)
	{
		switch(RNG.nextInt(3))
		{
			case 0: return(new Sword(l));
			case 1: return(new Shield(l));
			case 2: return(new Robes(l));
			default: return null;
		}
	}
}
